// pages/addingvisitors/addingvisitors.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    familyID: "",
    name: "",
    phone: "",
    sex: "",
    idCard: "",
    isDefault: "",
    cardTypeList:[],
    cardTypeNameList:[],
    cardTypeNameIndex: 0,
    currentType: 'ID'
  },


  formSubmit: function () {
    wx.navigateTo({
      url: '../patient/patient'
    })

    wx.showToast({
      title: '保存成功',
      icon: 'none',
      duration: 2000
    })

  },

  /**
   * 设置姓名
   */
  setName(e) {
    this.setData({
      name: e.detail.value
    })
  },

  /**
   * 设置候诊人电话号码
   */
  setPhone(e) {
    this.setData({
      phone: e.detail.value
    })
  },

  /**
   * 设置候诊人身份证号码
   */
  setIdCard(e) {
    this.setData({
      idCard: e.detail.value
    })
  },

  /**
   * 设置是否为默认候诊人
   */
  setDefault(e) {
    this.setData({
      isDefault: e.detail.value ? 1 : 0,
    })
  },

  /**
   * 设置候诊人性别
   */
  setSex(e) {
    console.log(e.detail)
    this.setData({
      sex: e.detail.value,
    })
  },
  toast(img) {
    wx.showToast({
      title: img,
      icon: 'none',
      duration: 2000
    })
  },

  /**
   * 保存就诊人信息
   */
  savePatien() {
    var name = this.data.name
    var phone = this.data.phone
    if (!name) {
      this.toast('请输入姓名!')
      return false;
    }
    var phoneReg = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/;
    if (!phoneReg.test(phone)) {
      this.toast('手机号不规范！');
      return false
    }
    var user = wx.getStorageSync("user")
    var userID = user.userID;
    var idCard = this.data.idCard;
    var idcardReg = /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
    var cardType = this.data.cardTypeList[this.data.cardTypeNameIndex].passty
    if (cardType == 'ID' && !idcardReg.test(idCard)) {
      this.toast('身份证号不规范！')
      return false
    }
    var isDefault = this.data.isDefault
    var sex = this.data.sex
    var options = {
      familyName: name,
      familyPhone: phone,
      sex: sex,
      familyNo: idCard,
      isDefault: isDefault,
      userID: userID,
      familyNoType: this.data.currentType
    }
    var familyID = this.data.familyID
    console.log(familyID)
    //判断页面加载为编辑还是新增
    if (familyID != null && familyID != "" && familyID != 'undefined') {
      console.log("修改就诊人信息")
      options.familyID = familyID;
      console.log(options)
      wx.request({
        url: getApp().globalData.myHttp + '/family/update',
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: options,
        success: (result) => {
          console.log(result)
          if (result.data.success) {
            wx.navigateBack()
            wx.showToast({
              title: '修改成功',
              icon: 'none',
              duration: 2000
            })
          } else {
            this.toast(result.data.msg)
          }
        }
      })
    } else {
      console.log("新增就诊人信息")
      wx.request({
        url: getApp().globalData.myHttp + '/family/save',
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: options,
        success: (result) => {
          console.log(result)
          if (result.data.success) {
            wx.navigateBack()
            wx.showToast({
              title: '保存成功',
              icon: 'none',
              duration: 2000
            })
          } else {
            wx.showToast({
              title: result.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var familyID = options['familyID']
    
    //判断页面加载为编辑还是新增
    if (familyID != null && familyID != "" && familyID != 'undefined') {
      wx.request({
        url: getApp().globalData.myHttp + '/family/getById/' + familyID,
        method: "GET",
        header: {
          'content-type': 'application/json'
        },
        success: (result) => {
          // console.log(JSON.stringify(result))
          if (result.data.success && result.data.obj != null) {
            console.log(JSON.stringify(result))
            //查询成功
            this.setData({
              name: result.data.obj.familyName,
              phone: result.data.obj.familyPhone,
              idCard: result.data.obj.familyNo,
              sex: result.data.obj.sex,
              isDefault: result.data.obj.isDefault,
              familyID: result.data.obj.familyID,
              currentType:result.data.obj.familyNoType
            })
            this.getPasstyList();
          } else {
            console.log("查询失败")
            wx.showToast({
              title: '查询失败',
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    } else {
      this.getPasstyList();
      this.setData({
        sex: 0,
        isDefault: 0,
      })
    }
  },

  //获取证件类型
  getPasstyList(){
    wx.request({
      url: getApp().globalData.myHttp + '/family/getPasstyList',
        method: "GET",
        header: {
          'content-type': 'application/json'
        },
        success: (result) => {
              console.log(JSON.stringify(result))
              var cardTypeNameList = []
              var cardTypeList = result.data.obj
              var defaultCardIndex = 0
              for (let i in cardTypeList) {
                cardTypeNameList.push(cardTypeList[i].passtytxt)
                if(this.data.currentType == cardTypeList[i].passty){
                  defaultCardIndex = i;
                }
              }
              if(result.data.success){
                  this.setData({
                    cardTypeList: cardTypeList,
                    cardTypeNameList: cardTypeNameList,
                    cardTypeNameIndex: defaultCardIndex,
                    currentType: cardTypeList[defaultCardIndex].passty
                  })
              }
        }
    })
  },
  
  bindPickChange(e){
    var index = e.detail.value;
    console.log("选择证件类型", this.data.cardTypeList[index])
    this.setData({
      cardTypeNameIndex: index,
      currentType: this.data.cardTypeList[index].passty
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  perserve: function () {

  }
})